package com.bibrApp.difu.actvity.security_strategy_mvp;


import android.content.Context;
import android.content.Intent;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.bibrApp.difu.Config;
import com.bibrApp.difu.DfApp;
import com.bibrApp.difu.R;
import com.bibrApp.difu.Response.LoginResponseBody;
import com.bibrApp.difu.base.BaseActivity;
import com.bibrApp.difu.utils.BiBrShare;
import com.bibrApp.difu.utils.NetWorkUtil;
import com.bibrApp.difu.utils.ShareUtils;
import com.bibrApp.difu.utils.ToastUtils;
import com.gyf.barlibrary.ImmersionBar;


import androidx.annotation.Nullable;
import androidx.appcompat.widget.SwitchCompat;
import butterknife.BindView;
import butterknife.OnClick;

public class SecurityActivity extends BaseActivity<SecurityPresenter> implements ISecurityView {

    private ImmersionBar mImmersionBar;
    private Context mContext;
    @BindView(R.id.security_switch)
    SwitchCompat  security_switch;
    @BindView(R.id.radioGroupId)
    RadioGroup radioGroup;
    @BindView(R.id.one_rb)
    RadioButton one_rb;
    @BindView(R.id.every_rb)
    RadioButton every_rb;
    private boolean isNotNeedHttps;
    private ShareUtils biBrShare;

    @Override
    protected void initPresenter() {
        presenter = new SecurityPresenter(this, this);
        presenter.init();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_security;
    }

    @Override
    public void initView() {
        mContext = this;
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.statusBarColor(R.color.white)
                .statusBarDarkFont(true)
                .fitsSystemWindows(true)
                .init();

        DfApp dfApp = (DfApp) getApplication();
        biBrShare = BiBrShare.getShare(mContext, Config.FILE_ACCOUNT);
        if(dfApp.getmData() != null){
            if(dfApp.getmData().getCenterUserMain().getPasswordStrategy() == 0){//启用
                security_switch.setChecked(true);
            }else{
                security_switch.setChecked(false);
            }
            if(dfApp.getmData().getCenterUserMain().getPasswordStatus() == 0){//第一次输入资金安全码
                one_rb.setChecked(true);
            }else{
                every_rb.setChecked(true);
            }
        }
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(!isNotNeedHttps){
                    if(checkedId == R.id.one_rb){
                        updateSecurityStrategy("",security_switch.isChecked()?"1":"0");
                    }else if(checkedId == R.id.every_rb){
                        updateSecurityStrategy("",security_switch.isChecked()?"0":"1");
                    }
                }else{
                    isNotNeedHttps = false;
                }
            }
        });
        security_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!isNotNeedHttps) {
                    if (isChecked) {
                        updateSecurityStrategy("0","");
                    } else {
                        updateSecurityStrategy("1","");
                    }
                } else {
                    isNotNeedHttps = false;
                }
            }
        });
    }

    private void updateSecurityStrategy(String strategy , String status){

        if(NetWorkUtil.isNetWorkConnected(mContext)){
            presenter.setSecurityStrategy(status,strategy);
        }else{
            isNotNeedHttps = true;
            one_rb.setChecked(!one_rb.isChecked());
            every_rb.setChecked(!every_rb.isChecked());
            ToastUtils.ShowToast(mContext,"网络连接断开，请您检查网络");
        }

    }
    @OnClick(R.id.return_iv)
    public void onClickBack(){
        finish();
    }
    @Override
    public void error(String msg) {
        ToastUtils.ShowToast(mContext,msg);
        isNotNeedHttps = true;
        one_rb.setChecked(!one_rb.isChecked());
        security_switch.setChecked(!security_switch.isChecked());
    }

    @Override
    public void success(String msg) {
        ToastUtils.ShowToast(mContext,msg);
        presenter.getUserInfo(app.getUserId());
    }

    @Override
    public void successOfGetUserInfo(LoginResponseBody.CenterUserMain userInfo) {
        LoginResponseBody data = app.getmData();
        if (userInfo != null) {
            biBrShare.deleteData(Config.FILE_ACCOUNT_KEY);
            data.setCenterUserMain(userInfo);
            biBrShare.saveData(data, Config.FILE_ACCOUNT_KEY);
        }
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mImmersionBar != null) {
            mImmersionBar.destroy();
        }

    }

}
